package com.rose.redis._distributed_lock;

import com.rose.redis._distributed_lock.impl.LockFactory;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;

import java.time.LocalDateTime;

/**
 * <p>
 * t1线程
 * </p>
 *
 * @author: zhu.chen
 * @date: 2020/12/31
 * @version: v1.0.0
 */
@Slf4j
//@Component
public class TestRedissonLockCommandLineRunner implements CommandLineRunner, Ordered {

    @Autowired
    private Lock lock;

    @Override
    public void run(String... args) throws Exception {
        RLock rLock = LockFactory.getReentrantLock();
        Thread t1 = new Thread(() -> {
            // time 1 : 2020-12-31T10:39:47.972
            System.out.println("time 1 : " + LocalDateTime.now().toString());
            try {
                lock.tryLock(rLock);
            } catch (InterruptedException e) {
                log.error(e.getMessage(), e);
            }
            // time 2 : 2020-12-31T10:39:47.981
            System.out.println("time 2 : " + LocalDateTime.now().toString());
        });
        t1.start();
        lock.tryLock(rLock);
        //time 3 : 2020-12-31T10:39:57.987
        System.out.println("time 3 : " + LocalDateTime.now().toString());
    }

    @Override
    public int getOrder() {
        return 1;
    }

}
